//
//  MenuView.m
//  HospitalSystem
//
//  Created by jss on 16/5/4.
//
//

#import "MenuView.h"
#import "CZCollectionViewCell.h"
#import "CZBtnModel.h"

#define SCREENW [UIScreen mainScreen].bounds.size.width

#define random(r,g,b,a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/255.0]

#define randomColor random(arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255), arc4random_uniform(255))

@interface MenuView () <UICollectionViewDelegate,UICollectionViewDataSource>


@property(nonatomic,strong)NSArray *btnArray;

@property(nonatomic,strong)UIView *line;

@property(nonatomic,assign)NSInteger index;

@end

@implementation MenuView


//懒加载
-(NSArray *)btnArray{
    
    if (_btnArray == nil) {
   
    //读取文件路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"button.plist" ofType:nil];
    //读取文件
    NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];
    //创建临时数组
    NSMutableArray *mArray = [NSMutableArray array];
    //遍历数组 字典转模型
    for (NSDictionary *dict in tempArray) {
        
        CZBtnModel *model = [CZBtnModel btnModel:dict];
        
        [mArray addObject:model];
    }
    
        _btnArray = mArray.copy;
   }
    return _btnArray;
}



-(void)awakeFromNib {

    self.delegate = self;
    self.dataSource = self;
    
    self.backgroundColor = [UIColor whiteColor];
    
    //注册cell
    [self registerClass:[CZCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    

}


//布局子控件
-(void)layoutSubviews {
    
    [super layoutSubviews];
    
//    self.index = 0;
//    for (int i = 0; i <2; i++) {
//        
//        UIView *lineView = [[UIView alloc]init];
//        
//        lineView.backgroundColor = [UIColor grayColor];
//        
//        self.line = lineView;
//        
//        self.line.frame = CGRectMake((self.index + 1) * SCREENW / 3 , 0, 1, self.frame.size.height);
//        
//        NSLog(@"%f",self.frame.size.height);
//        
//        [self addSubview:lineView];
//        self.index ++;
//    }
    
    [self createSeparatLint:CGRectMake(1 * SCREENW / 3, 0, 1, self.frame.size.height)];
    [self createSeparatLint:CGRectMake(2 * SCREENW / 3, 0, 1, self.frame.size.height)];
    [self createSeparatLint:CGRectMake(0, 0, SCREENW, 1)];
    [self createSeparatLint:CGRectMake(0, self.frame.size.height / 2, SCREENW, 1)];
    
    
    UICollectionViewFlowLayout *flowLayout =  (UICollectionViewFlowLayout*)self.collectionViewLayout;
    
    flowLayout.itemSize = CGSizeMake(SCREENW / 3, self.frame.size.height / 2);
    
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.minimumLineSpacing = 0;
    
}

-(void)createSeparatLint:(CGRect) rect {
    
    //self.index = 0;
    
  //  for (int i = 0; i <2; i++) {
        
        UIView *lineView = [[UIView alloc]init];
        
        lineView.backgroundColor = [UIColor grayColor];
        
//        self.line = lineView;
//        
//        self.line.frame = rect;
       lineView .frame = rect;
        
       // NSLog(@"%f",self.frame.size.height);
        
        [self addSubview:lineView];
        
        //self.index ++;
   // }//

    
}

//设置组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
//设置多少个cell
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.btnArray.count;
}

//设置每个cell的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CZBtnModel *model = self.btnArray[indexPath.row];
    
    CZCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
//    cell.backgroundColor = randomColor;
    
    cell.model = model;
    
    return cell;
    
}

@end
